<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from "better-scroll";

export default {
  name: "Scroll",
  data() {
    return {
      scroll: null,
      probeType: {
        type: Number,
        default: 0,
      },
      pullUpLoad: {
        type: Boolean,
        default: false,
      },
    };
  },
  mounted() {
    this.init();

    // this.scrollTo(0, 0);
    this.scroll.on("scroll", (position) => {
      this.$emit("scroll", position);
    });

    this.scroll.on("pullingUp", () => {
      this.$emit("pullingUp");
    });
  },
  destroyed() {
    console.log('home destory');
  },
  methods: {
    init() {
      let scroll = new BScroll(this.$refs.wrapper, {
        probeType: this.probeType,
        pullUpLoad: this.pullUpLoad,
        observeDOM: true,
        observeImage: true,
        click: true, //BetterScroll 默认会阻止浏览器的原生 click 事件。如果你想要 click 事件生效，BetterScroll 会派发一个 click 事件，并且 event 参数的 _constructed 为 true。
        mouseWheel: true, //开启鼠标滚轮
      });
      this.scroll = scroll;
    },

    scrollTo(x, y, time = 300) {
      this.scroll.scrollTo(x, y, time);
    },

    finishPullUp() {
      return this.scroll.finishPullUp();
    },
  },
};
</script>
<style scoped></style>
